#
# ttkiosk - table tennis club touch screen based kiosk.
# Copyright (C) 2009  Sergey Satskiy
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA  02110-1301, USA.
#
# $Id: AdminTopBar.py 38 2009-11-14 05:33:52Z sergey.satskiy $
#
# Generated by PyQt4 UI code generator 4.5.4 and then customized
#


from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import SIGNAL, SLOT
from utils import debugMsg, GlobalData, collectHostInfo
import ui, utils, os

class Ui_AdminTopBar(ui.FormBaseClass):
    def setupUi(self, AdminTopBar):
        ui.FormBaseClass.__init__(self)

        AdminTopBar.setObjectName("AdminTopBar")
        AdminTopBar.resize(795, 206)
        self.horizontalLayoutWidget = QtGui.QWidget(AdminTopBar)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(10, 10, 753, 80))
        self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
        self.horizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setObjectName("horizontalLayout")
        spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem)
        self.createEventButton = QtGui.QPushButton(self.horizontalLayoutWidget)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Maximum)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.createEventButton.sizePolicy().hasHeightForWidth())
        self.createEventButton.setSizePolicy(sizePolicy)
        self.createEventButton.setMinimumSize(QtCore.QSize(0, 40))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setWeight(75)
        font.setBold(True)
        self.createEventButton.setFont(font)
        self.createEventButton.setFocusPolicy(QtCore.Qt.NoFocus)
        self.createEventButton.setObjectName("createEventButton")
        self.horizontalLayout.addWidget(self.createEventButton)
        self.toUserButton = QtGui.QPushButton(self.horizontalLayoutWidget)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Maximum)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.toUserButton.sizePolicy().hasHeightForWidth())
        self.toUserButton.setSizePolicy(sizePolicy)
        self.toUserButton.setMinimumSize(QtCore.QSize(0, 40))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setWeight(75)
        font.setBold(True)
        self.toUserButton.setFont(font)
        self.toUserButton.setFocusPolicy(QtCore.Qt.NoFocus)
        self.toUserButton.setObjectName("toUserButton")
        self.horizontalLayout.addWidget(self.toUserButton)
        self.shutdownButton = QtGui.QPushButton(self.horizontalLayoutWidget)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Maximum)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.shutdownButton.sizePolicy().hasHeightForWidth())
        self.shutdownButton.setSizePolicy(sizePolicy)
        self.shutdownButton.setMinimumSize(QtCore.QSize(0, 40))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setWeight(75)
        font.setBold(True)
        self.shutdownButton.setFont(font)
        self.shutdownButton.setFocusPolicy(QtCore.Qt.NoFocus)
        self.shutdownButton.setObjectName("shutdownButton")
        self.horizontalLayout.addWidget(self.shutdownButton)
        self.restartButton = QtGui.QPushButton(self.horizontalLayoutWidget)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Maximum)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.restartButton.sizePolicy().hasHeightForWidth())
        self.restartButton.setSizePolicy(sizePolicy)
        self.restartButton.setMinimumSize(QtCore.QSize(0, 40))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setWeight(75)
        font.setBold(True)
        self.restartButton.setFont(font)
        self.restartButton.setFocusPolicy(QtCore.Qt.NoFocus)
        self.restartButton.setObjectName("restartButton")
        self.horizontalLayout.addWidget(self.restartButton)
        self.flushButton = QtGui.QPushButton(self.horizontalLayoutWidget)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Maximum)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.flushButton.sizePolicy().hasHeightForWidth())
        self.flushButton.setSizePolicy(sizePolicy)
        self.flushButton.setMinimumSize(QtCore.QSize(0, 40))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setWeight(75)
        font.setBold(True)
        self.flushButton.setFont(font)
        self.flushButton.setFocusPolicy(QtCore.Qt.NoFocus)
        self.flushButton.setObjectName("flushButton")
        self.horizontalLayout.addWidget(self.flushButton)
        self.showHostInfoButton = QtGui.QPushButton(self.horizontalLayoutWidget)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Maximum)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.showHostInfoButton.sizePolicy().hasHeightForWidth())
        self.showHostInfoButton.setSizePolicy(sizePolicy)
        self.showHostInfoButton.setMinimumSize(QtCore.QSize(0, 40))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setWeight(75)
        font.setBold(True)
        self.showHostInfoButton.setFont(font)
        self.showHostInfoButton.setFocusPolicy(QtCore.Qt.NoFocus)
        self.showHostInfoButton.setObjectName("showHostInfoButton")
        self.horizontalLayout.addWidget(self.showHostInfoButton)
        spacerItem1 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem1)

        self.retranslateUi(AdminTopBar)
        QtCore.QMetaObject.connectSlotsByName(AdminTopBar)

    def retranslateUi(self, AdminTopBar):
        AdminTopBar.setWindowTitle(QtGui.QApplication.translate("AdminTopBar", "AdminTopBar", None, QtGui.QApplication.UnicodeUTF8))
        self.createEventButton.setText(QtGui.QApplication.translate("AdminTopBar", "Create New Event", None, QtGui.QApplication.UnicodeUTF8))
        self.toUserButton.setText(QtGui.QApplication.translate("AdminTopBar", "Switch to User Mode", None, QtGui.QApplication.UnicodeUTF8))
        self.shutdownButton.setText(QtGui.QApplication.translate("AdminTopBar", "Shutdown", None, QtGui.QApplication.UnicodeUTF8))
        self.restartButton.setText(QtGui.QApplication.translate("AdminTopBar", "Restart Kiosk", None, QtGui.QApplication.UnicodeUTF8))
        self.flushButton.setText(QtGui.QApplication.translate("AdminTopBar", "Flush Cache", None, QtGui.QApplication.UnicodeUTF8))
        self.showHostInfoButton.setText(QtGui.QApplication.translate("AdminTopBar", "Show host info", None, QtGui.QApplication.UnicodeUTF8))


class AdminTopBar(QtGui.QWidget, Ui_AdminTopBar):
    def __init__(self, path, parent=None, f=QtCore.Qt.WindowFlags()):
        QtGui.QWidget.__init__(self, parent, f)

        self.setupUi(self)
        self.makeConnections()
        return


    def makeConnections( self ):
        """ A collection of SIGNAL - SLOT connections """

        globalData = GlobalData()
        self.connect( self.restartButton, SIGNAL("clicked()"),
                      globalData.application, SLOT("quit()") )
        self.connect( self.showHostInfoButton, SIGNAL("clicked()"),
                      self.showHostInfoCkicked )
        self.connect( self.createEventButton, SIGNAL("clicked()"),
                      self.createEventButtonClicked )
        self.connect( self.toUserButton, SIGNAL("clicked()"),
                      self.toUserButtonClicked )
        self.connect( self.shutdownButton, SIGNAL("clicked()"),
                      self.shutdownButtonClicked )
        self.connect( self.flushButton, SIGNAL("clicked()"),
                      self.flushButtonClicked )
        return


    def setLayoutGeometry( self, width, height ):
        """ updates the whole form layout size """

        self.horizontalLayoutWidget.setGeometry( QtCore.QRect( 0, 0,
                                                               width, height ) )
        return


    def showHostInfoCkicked( self ):
        """ processing click() on the Show host info button """

        QtGui.QMessageBox.information( None, "Host Information",
                                       "<html>" \
                                       "<pre>" + collectHostInfo() + "</pre>" \
                                       "</html>" )
        return


    def createEventButtonClicked( self ):
        """ processing click() on the Create event button """

        QtGui.QMessageBox.information( None, "Not implemented yet",
                                       "Not implemented yet" )
        return


    def toUserButtonClicked( self ):
        """ processing click() on the To User Mode button """

        GlobalData().isAdmin = False
        ui.hideForm( 'AdminTopBar' )
        ui.showForm( 'TopBar' )
        return


    def shutdownButtonClicked( self ):
        """ processing click() on the Shutdown button """

        os.system( "sudo poweroff" )
        return


    def flushButtonClicked( self ):
        """ processing click() on the Flush button """

        debugMsg( "Sending a test notification" )
        GlobalData().application.sendNotification( "something" )
        debugMsg( "Sent" )

        QtGui.QMessageBox.information( None, "Not implemented yet",
                                       "Not implemented yet" )
        return



